package com.wwwc.index.web.servlet;

import java.util.*;
import javax.servlet.http.*;
import java.rmi.RemoteException;

import com.wwwc.util.web.*;
//import com.wwwc.index.web.database.*;
import com.wwwc.index.web.ejb.database.*;

public class SendMailBean {
    private StringBuffer message;
    private EJBDatabaseQuery ejbdb;

    public StringBuffer mailForm(String sid, String form_call_back) {

	StringBuffer sbf1 = new StringBuffer();
	sbf1.append("\n<TABLE cellSpacing=0 cellPadding=0 border=0 width=100% style='font-size: 10pt'>");
	sbf1.append("\n<TR><TD align=center style='font-size: 12pt'>");
	sbf1.append("\nAccount/E-mail");
	sbf1.append("\n</TD><TR>");
	sbf1.append("\n</TABLE>");

	StringBuffer sbf3 = new StringBuffer();

	StringBuffer sbf2 = new StringBuffer();
	sbf2.append("\n\n<FORM method=post action="+form_call_back+">");
	sbf2.append("\n<TABLE cellSpacing=0 cellPadding=0 border=0 width=100% style='font-size: 10pt'>");
	sbf2.append("\n<TR><TD align=center style='font-size: 14pt'>");
	sbf2.append("\n<input type=submit value=Send style='font-size: 8pt'>");
	sbf2.append("\n<input type=reset value=Reset style='font-size: 8pt'>");
	sbf2.append("\n</TD><TR>");
	sbf2.append("\n</TABLE>");
	sbf3.append(sbf2);

	sbf3.append("\n<TABLE cellSpacing=0 cellPadding=10 border=0 width=100% style='font-size: 10pt'>");

	sbf3.append("\n<tr>");
	sbf3.append("\n<td align=right style='font-size: 12pt'>To:</td>");
	sbf3.append("\n<td align=left><input type=text name=mailto size=80 maxLength=200></td>");
	sbf3.append("\n</tr>");

	sbf3.append("\n<tr>");
	sbf3.append("\n<td align=right style='font-size: 12pt'>Cc:</td>");
	sbf3.append("\n<td align=left><input type=text name=mailcc size=80 maxLength=200></td>");
	sbf3.append("\n</tr>");

	sbf3.append("\n<tr>");
	sbf3.append("\n<td align=right style='font-size: 12pt'>Subject:</td>");
	sbf3.append("\n<td align=left><input type=text name=mailsubject size=80 maxLength=200></td>");
	sbf3.append("\n</tr>");

	sbf3.append("\n<tr>");
	sbf3.append("\n<td>&nbsp;</TD>");
	sbf3.append("\n<td><textarea name=mailcontent rows=10 cols=60></textarea></td>");
	sbf3.append("\n</tr>");

	sbf3.append("\n</TABLE>");
	sbf3.append("\n<input type=hidden name=fid value="+sid+">");
	sbf3.append("\n</FORM>\n");
	

	StringBuffer sbf = new StringBuffer();	
	//sbf.append(MyHtml.forumDisplay ("E-mail Form", sbf1.toString(), "", "", sbf3, "center", "middle", "", ""));
	return sbf3;
    }


    public boolean checkUsernamePassword(String username, String password) {
	if(username != null && password != null && username.length()>1 && password.length()>1) {
	    username = username.toUpperCase();
	}
	else {
	    return false;
	}
	if(ejbdb == null) {
	    ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	}
	try {
	    String temp = ejbdb.selectSingleFieldValue("SELECT PASSWD FROM USERS WHERE USERNAME='"+username+"';","PASSWD");
	    if(temp != null && temp.length()>1 && temp.equals(password)) {
		return true;
	    }
	}
	catch(RemoteException se) {
	    return false;
	}
	return false;
    }

    public boolean setUserProfile (HttpSession session, String username, String user_ip) {
	if(ejbdb == null) {
	    ejbdb = (new DatabaseEJBQuery()).getEJBDatabaseQuery();
	}

	try {
	    String query = "SELECT * FROM USER_INFO WHERE USERNAME='"+username.toUpperCase()+"' ORDER BY JOINDAY;";
	    Hashtable temphash = ejbdb.selectSingleRecordHashtable(query);

	    if(temphash != null) {
		if(temphash.get("STATUS").equals("N")) {
		    message = new StringBuffer();
		    message.append("Activation required!");
		    return false;
		}

		Hashtable user_info = new Hashtable();
		user_info.put("user_name", temphash.get("USERNAME"));
		user_info.put("user_age", MyDateAndTime.getUserAge((String)temphash.get("BIRTHDAY")));
		user_info.put("user_email", temphash.get("EMAIL"));
		user_info.put("user_fname", temphash.get("FIRSTNAME"));
		user_info.put("user_lname", temphash.get("LASTNAME"));
		user_info.put("user_since", temphash.get("JOINDAY"));
		user_info.put("user_sex", temphash.get("GENDER"));
		user_info.put("user_edu", temphash.get("EDUCATION"));
		user_info.put("user_zip", temphash.get("ZIPCODE"));
		user_info.put("user_level", temphash.get("FIELD11"));
		user_info.put("last_login", temphash.get("FIELD13"));
		user_info.put("last_ip", temphash.get("FIELD14"));
		message = new StringBuffer("Last login:"+temphash.get("FIELD13")+" From:"+temphash.get("FIELD14"));

		session.setAttribute("user_info",user_info);

		query = "UPDATE USER_INFO SET "+
		    "FIELD12='"+session.getId()+"',"+
		    "FIELD13='"+System.currentTimeMillis()+"',"+
		    "FIELD14='"+user_ip+"' "+
		    " WHERE USERNAME='"+username.toUpperCase()+"' AND STATUS='Y';";
		ejbdb.update(query);
	    }
	    else {
		message = new StringBuffer("Please try again! Error[201].");
		return false;
	    }
	}
	catch(RemoteException se) {
	    message = new StringBuffer("Please try again! Error[202]");
	    return false;
	}

	return true;
    }

    public StringBuffer resetUrl (String url) {
	StringBuffer sbf = new StringBuffer();
	sbf.append("<HTML><HEAD>");
	sbf.append("<TITLE>user sign in</TITLE>");
	sbf.append("</HEAD>");
	sbf.append("<SCRIPT>");
	sbf.append("function DoLogIn() {");
	sbf.append("  top.location.replace(\""+url+"\");");
	sbf.append("  return true;");
	sbf.append("}");
	sbf.append("</SCRIPT>");
	sbf.append("<BODY onLoad=DoLogIn();>");
	sbf.append("</BODY></HTML>");
	return sbf;
    }

    public StringBuffer getMessage() {
	return message;
    }
    /*
    public StringBuffer sendEmail(HttpSession session) {
	StringBuffer sbf = new StringBuffer();
	sbf.append("<HTML><HEAD><TITLE>Resend email</TITLE>\n");
	sbf.append("</HEAD><BODY><CENTER>");
	sbf.append("<H3>A verification e-mail has been sent to:["+session.get("user_email")+"]<BR>");
	sbf.append("Please open your e-mail and click the link that 3WCenter.com sent to you.<BR>");
	sbf.append("</FORM></CENTER></BODY></HTML>");

	String from    = "E-mail.Verification@3wcenter.com";
	String to      = email;
	String subject = "3WCenter E-mail Verification";        
	String content = null;
	SendMail smail = new SendMail ();
	String return_url = null;
	if(user_ip.startsWith(SystemInfo.LOCAL_IPS)) {
	    return_url = SystemInfo.LOCAL_URL_IP;
	}
	else {
	    return_url = SystemInfo.SERVER_URL;
	}

	String content = "<HTML><HEAD><TITLE></TITLE></HEAD><BODY>"+
	    "<H2>Welcome!</H2><BR>\n"+
	    "Please verify your account information:<BR>"+
	    "<BR>User name:&nbsp;"+user_name+
	    "<BR>User age group:&nbsp;"+user_age+
	    "<BR>You can modify your account information after you sign in.<BR><BR>"+
	    "<A href="+return_url+"/servlet/UserRegisterServlet?action=verify&email="+
	    user_email+"&user_name="+user_name+"&user_ip="+user_ip+"&v_code="+invoice+
	    ">Please click this link to verify your account"+
	    " information</A><BR>"+
	    "If you can not see the above link, please type:<BR><BR>\n\n"+
	    return_url+"/servlet/UserRegisterServlet?action=verify&email="+
	    user_email+"&user_name="+user_name+"&user_ip="+user_ip+"&v_code="+invoice+"<BR><BR>\n\n"+
	    "in your browser address field to verify.<BR>\nThank your!<BR>\n--3WCenter.com"+
	    "</BODY></HTML>";

	smail.setEmailFrom(from);
	smail.setEmailTo(to);
	smail.setEmailSubject(subject);
	smail.setEmailBody(content);
	String ms = smail.send();
    }
    */
    /*
      else {
      PasswdFileLoader ps_file_loader = new PasswdFileLoader();
      ps_file_loader.setFileName(new File("D:/myServers/ApacheGroup/Apache2/passwd", ".htaccess1"));
      ps_file_loader.loadFile();
      ps_ht = ps_file_loader.getHashtable();
      
      if (ps_ht.containsKey(user_name)) {
      httpConnecter c = new httpConnecter ();
      c.setUrl("http://192.168.1.103"); //send to authantication server
      c.setUserName(user_name);
      c.setPassWord(pass_word);
      
      if (((String)session.getAttribute("use_pin")).equals("true")) {
      Hashtable pin_hashtable = new Hashtable ();
      ps_file_loader.setFileName(new File("D:/myServers/ApacheGroup/Apache2/passwd", ".htaccess0"));
      ps_file_loader.loadFile();
      pin_hashtable = ps_file_loader.getHashtable();
      
      if(!pin_hashtable.get(session.getAttribute("pin_key")).equals(request.getParameter("pin"))) {
      RequestDispatcher rd = 
      request.getRequestDispatcher("/servlet/MainBarServlet?action=login_error"); 
      rd.forward(request,response);
      return;
      }
      }
      
      c.connecting();
      
      String conn_message   = c.getConnMessage();
      if (conn_message.equals("ResponseCode=200ResponseMessage=OK")) {
      user_info.put("user_name",user_name);
      user_info.put("user_age",user_age);
      user_info.put("user_level",user_level);
      user_info.put("user_ip",user_ip);
      user_info.put("view_age",view_age);
      session.setAttribute("user_info", user_info);
      session.setAttribute("count",null);
      out.println("AAAAAA"+user_info.toString());
      }
      else {
      RequestDispatcher rd = 
      request.getRequestDispatcher("/servlet/MainBarServlet?action=login_error"); 
      rd.forward(request,response);
      }
      }
      else {
      RequestDispatcher rd = request.getRequestDispatcher("/servlet/MainBarServlet?action=login_error"); 
      rd.forward(request,response);
      }
      }//paied user
      }
      else { // user_name or password error
      RequestDispatcher rd = request.getRequestDispatcher("/servlet/MainBarServlet?action=login_error"); 
      rd.forward(request,response);
      }
      
      }
      catch(Exception e) {
      return;
      }

      }
    */
}







